package com.sanyang.pda.ui.manualoutbound;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sanyang.pda.adapter.ScanLocationAdapter;
import com.sanyang.pda.bean.ByBarcodeBean;
import com.sanyang.pda.bean.ScanBean;
import com.sanyang.pda.bean.SelectBean;
import com.sanyang.pda.bean.request.AddConfirmRequest;
import com.sanyang.pda.contract.ScanLocationContract;
import com.sanyang.pda.dialog.ScanGoodsDialog;
import com.sanyang.pda.dialog.SelectPop;
import com.sanyang.pda.presenter.ScanLocationPresenter;
import com.sanyang.pda.utils.StringConfig;
import com.sanyang.pda.utils.UserUtils;
import com.sanyang.pda.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.projectbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
@Route(path = AppActivityKey.ADDGOODSACTIVITY)
public class AddGoodsActivity extends BaseMvpActivity<ScanLocationPresenter> implements ScanLocationContract.View {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.title)
    FrameLayout title;
    @BindView(R.id.tv_tray)
    TextView tvTray;
    @BindView(R.id.tv_clear)
    ScanButView tvClear;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.bt_scan_location)
    Button btScanLocation;
    ScanLocationAdapter scanLocationAdapter;
    private String storagebinId;
    private String deliveryNo;
    @Override
    public ScanLocationPresenter getPresenter() {
        return new ScanLocationPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_scan_location;
    }

    @Override
    public void initView() {
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        scanLocationAdapter=new ScanLocationAdapter(R.layout.item_scan_location,new ArrayList<>());
        rvList.setAdapter(scanLocationAdapter);
    }

    @Override
    public void initLinsenterner() {
        tv1.setText(storagebinId);
        tvClear.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                if (scanBean.getType() == -1){
                    tvTray.setText(scanBean.getCode());
                    presenter.selectArticleInfoByBarcode(scanBean.getCode(),deliveryNo);
                }
            }
        });
    }

    @Override
    public void getData() {
        Bundle bundle=getIntent().getExtras();
        storagebinId=bundle.getString(StringConfig.STORAGEBINID);
        deliveryNo=bundle.getString(StringConfig.DELIVERYNO);

    }


    @OnClick({R.id.iv_back, R.id.bt_scan_location})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finishActivity();
                break;
            case R.id.bt_scan_location:
            if (getList().size() == 0){
                showMessage(getString(R.string.goods) + getString(R.string.not_be_empty));
                return;
            }
            presenter.addConfirm(getList());
                break;
        }
    }
    private List<AddConfirmRequest> getList(){
        List<AddConfirmRequest> list=new ArrayList<>();
        for (ByBarcodeBean item: scanLocationAdapter.getData()){
            AddConfirmRequest addConfirmRequest=new AddConfirmRequest();
            addConfirmRequest.setArticleId(item.getArticleId());
            addConfirmRequest.setDeliveryNo(deliveryNo);
            addConfirmRequest.setStoragebinId(storagebinId);
            addConfirmRequest.setNum(item.getNum()+"");
            addConfirmRequest.setWaitoutOperation(UserUtils.getInstance().getUserBean().getUserName());
            list.add(addConfirmRequest);
        }
        return list;
    }
    List<SelectBean> selectBeanList=new ArrayList<>();
    @Override
    public void setGoodsList(List<ByBarcodeBean> list) {
        if (list.size() == 0){
            showMessage("未查询到商品信息");
            return;
        }
        if (list.size() == 1){
            ScanGoodsDialog scanGoodsDialog=new ScanGoodsDialog(mContext, new ScanGoodsDialog.ConfirmClick() {
                @Override
                public void onConfirmClick(ByBarcodeBean underDetectionReuqest) {
                    scanLocationAdapter.addData(underDetectionReuqest);
                }
            });
            scanGoodsDialog.show(list.get(0),deliveryNo);
        }
        if (list.size() > 1){
            selectBeanList.clear();
            for (int i=0;i< list.size();i++){
                ByBarcodeBean byBarcodeBean=list.get(i);
                SelectBean selectBean=new SelectBean(byBarcodeBean.getArticleName(),byBarcodeBean.getArticleId());
                selectBean.setOther(byBarcodeBean.getSpec());
                selectBeanList.add(selectBean);
            }
            SelectPop selectPop=new SelectPop(mContext, new SelectPop.Onclick() {
                @Override
                public void onClick(SelectBean content) {
                    ByBarcodeBean byBarcodeBean=new ByBarcodeBean();
                    byBarcodeBean.setArticleName(content.getName());
                    byBarcodeBean.setArticleId(content.getId());
                    byBarcodeBean.setSpec(content.getOther());
                    ScanGoodsDialog scanGoodsDialog=new ScanGoodsDialog(mContext, new ScanGoodsDialog.ConfirmClick() {
                        @Override
                        public void onConfirmClick(ByBarcodeBean underDetectionReuqest) {
                            scanLocationAdapter.addData(underDetectionReuqest);
                        }
                    });
                    scanGoodsDialog.show(byBarcodeBean,deliveryNo);
                }
            });
            selectPop.show(selectBeanList);
        }
    }


    @Override
    public void addSuccess() {
        finishActivity();
    }
}
